﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class EngineTorque : Collection<TorquePerRPM>
    {
        #region CTORs

        /// <summary>Creates a new engine torque</summary>
        public EngineTorque() { }

        /// <summary>Creates a new engine torque</summary>
        /// <param name="torquePerRPM"></param>
        public EngineTorque(IEnumerable<TorquePerRPM> torquePerRPM)
        {
            AddRange(torquePerRPM);
        }

        #endregion

        #region Methods

        public void Add(int rpm, int torque)
        {
            Add(new TorquePerRPM(rpm, torque));
        }

        public void AddRange(IEnumerable<TorquePerRPM> torquePerRPM)
        {
            foreach (var pair in torquePerRPM)
                Add(pair);
        }

        public void AddRange(params int[] torquePerRPM)
        {
            var rpm = int.MinValue;
            foreach (var val in torquePerRPM)
            {
                if (rpm == int.MinValue)
                {
                    rpm = val;
                    continue;
                }

                var torque = val;
                Add(rpm, torque);
                rpm = int.MinValue;
            }
        }

        public void Remove(int rpm)
        {
            for (var i = 0; i < Count; i++)
            {
                var torquePerRPM = this[i];
                if (torquePerRPM.RPM != rpm) continue;

                Remove(torquePerRPM);
                i--;
            }
        }

        #endregion
    }
}
